<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/10 0010
 * Time: 16:42
 */

namespace api\storage\service;


use api\storage\model\FeedDetailModel;
use api\storage\model\FeedStationModel;
use api\storage\model\MachineModel;
use api\storage\model\MachineSeatModel;
use api\storage\model\ReelsModel;
use api\storage\model\SmtMaterielModel;
use api\storage\model\SmtOutflowModel;
use api\storage\model\TransferModel;
use api\storage\common\Constant;
use cmf\lib\Storage;
use service\file\FileLock;
use service\logger\LoggerFactory;
use think\Db;
use think\Template;
use service\file\CommonFile;

class MachineServer extends BaseService
{
    protected $file;
    protected $transferModel;
    protected $feedDetailModel;
    protected $machineModel;
    protected $feedStationModel;
    protected $machineSeatModel;
    protected $smtMaterielModel;
    protected $reelsModel;
    protected $storageUuid;
    protected $smtOutflowModel;
    protected $logger;

    /**
     * MachineServer constructor.
     */
    public function __construct()
    {
        $this->transferModel = new TransferModel();
        $this->feedDetailModel = new FeedDetailModel();
        $this->machineModel = new MachineModel();
        $this->feedStationModel = new FeedStationModel();
        $this->machineSeatModel = new MachineSeatModel();
        $this->smtMaterielModel = new SmtMaterielModel();
        $this->reelsModel = new ReelsModel();
        $this->smtOutflowModel = new SmtOutflowModel();
        $this->logger = LoggerFactory::getLogger('machine');
    }

    /***
     * @param $reelId
     * @param string $storageUuid
     * @return array
     * @throws \think\Exception
     * 获取上料位置
     */
    public function getReelDestination($reelId, $storageUuid = "184928afb9e35f3ff421da05fc3f917f")
    {
        $where['transfer.reel_id'] = $reelId;
        $where['transfer.storage_uuid'] = $storageUuid;
        $result = $this->transferModel->findDestination($where);//中转区
        if (empty($result)) {
            return $this->resultTemplate(1);
        }
        $reel = $result->toArray();
        if ($reel['status'] == 1){
            return $this->resultTemplate(4);//该物料是普通退料
        }
        if ($reel['status'] == 2){
            return $this->resultTemplate(5);//该物料是切枪物料
        }
        $feed = $this->feedStationModel->findFeed(['feed_id' => $reel['feedId'], 'storage_uuid' => $storageUuid], '*')->toArray();

        $machine = $this->machineModel->findMachineInfo(['machine_id' => $reel['machineId'], 'storage_uuid' => $storageUuid])->toArray();

        if ($reel['machineStatus'] == 1 && $feed['uuid'] != $machine['feed_uuid']) {//如果机器上有其他工单正在生产的话，就返回错误
            return $this->resultTemplate(3);
        }


        $data = [
            'seatCode' => $reel['seatCode'],
            'machineId' => $reel['machineId'],
            'feedId' => $reel['feedId'],
        ];
        $this->logger->info("{$reel['feedId']} 获取上料位置 机器号为：{$reel['machineId']} 机器站位为{$reel['seatCode']}");
        return $this->resultTemplate(2, $data);


    }

    /****
     * @param $feedId
     * @param $machineId
     * @param $seatCode
     * @param $reelId
     * @param string $storageUuid
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 将物料放到机台上。
     */
    public function putMachine($feedId, $machineId, $seatCode, $reelId, $storageUuid = "184928afb9e35f3ff421da05fc3f917f")
    {

        $transferReel = $this->transferModel->findReel(['reel_id' => $reelId, 'storage_uuid' => $storageUuid]);

        $feed = $this->feedStationModel->findFeed(['feed_id' => $feedId, 'storage_uuid' => $storageUuid], '*')->toArray();
        $machine = $this->machineModel->findMachineInfo(['machine_id' => $machineId, 'storage_uuid' => $storageUuid])->toArray();

        if ($machine['status'] == 1 && $feed['uuid'] != $machine['feed_uuid']) {//如果机器上有其他的工单正在生产
            return $this->resultTemplate(3);
        }
        if (empty($machine['feed_uuid'])) {//如果机台上为空，则将当前工单号赋给机台
            $this->logger->debug("将工单号{$feed['feed_id']} 更新到机台{$machineId}");
            $this->machineModel->updateMachine(['machine_id' => $machineId, 'storage_uuid' => $storageUuid], ['status' => 1, 'feed_uuid' => $feed['uuid'], 'feed_id' => $feed['feed_id']]);
        }
        $where['machine_uuid'] = $machine['uuid'];
        $where['seat_code'] = $seatCode;
        $update = [
            'reel_id' => $transferReel['reel_id'],
            'count' =>  $transferReel['count'],
            'is_used' => 1,
            'sub_materiel_id' => $transferReel['sub_materiel_id'],
            'status' => 1,//设置为1是为了解锁物料站位，因为续料出库时锁定了货架
        ];

        $this->machineSeatModel->updateMachineSeat($where, $update);//将reel和count填到机台上，将is_use设置为1（正在使用
        $this->logger->info("将物料{$update['reel_id']}更新到机台{$machineId}的{$seatCode}号站位上 数量:{$transferReel['count']}");

        $this->transferModel->deleteByUuid($transferReel['uuid']);//删除中转表中的uuid
        return $this->resultTemplate(2);
    }

    /***
     * @param $userId
     * @param $storageUuid
     * @return array
     * 确认是否有未完成的续料
     */
    public function enterAddMateriel($userId, $storageUuid  ){
        $lock = new FileLock($storageUuid);
        $lock->lock(FileLock::LOCK_TYPE_MACHINE_ADD);//加文件锁
        $this->materielFile($userId, $storageUuid);
        $data = json_decode($this->file->read(), true);

        if (!empty($data)){
            $lock->unlock();
            $this->logger->info("用户：{$userId}之前的续料未完成");
            return $this->resultTemplate(1, $data);
        }
        $lock->unlock();
        return $this->resultTemplate(2);//文件不存在，所以该用户没有未完成的续料
    }

    /***
     * @param $reelId
     * @param $userId
     * @param string $storageUuid
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException 续料前的检查
     */
    public function addMaterielCheck($reelId, $userId ,$storageUuid = "184928afb9e35f3ff421da05fc3f917f")
    {


        $reel['reel_id'] = $reelId;
        $machineSeatInfo = $this->machineSeatModel->findMachineSeatInfo($reel);//根据reelId搜索
        if (empty($machineSeatInfo)) {//如果该物料不在机器上的话
            $this->logger->warn("续料检查失败，{$reelId}不在机台上");
            return $this->resultTemplate(3);
        }
        $machineSeat = $machineSeatInfo->toArray();
        if ($machineSeat['status'] == 0) {
            $this->logger->warn("续料检查失败，{$reelId}被锁定，机台号：{$machineSeat['machine_id']}，站位号：{$machineSeat['seat_code']}");
            return $this->resultTemplate(4);
        }
        $machine = $this->machineModel->findMachineInfo(['uuid' => $machineSeat['machine_uuid'], 'storage_uuid' => $storageUuid])->toArray();//寻找对应的机器
        $feedUuid = $machine['feed_uuid'];//该机器上正在生产的uuid
        $feed = $this->feedStationModel->findFeed(['uuid' => $feedUuid], 'feed_id')->toArray();//根据feed_uuid，获取当前正在生产的feed
        $where['feed_id'] = $feed['feed_id'];
        $where['machine_id'] = $machine['machine_id'];
        $where['seat_code'] = $machineSeat['seat_code'];
        $feedDetail = $this->feedDetailModel->findFeedDetail($where)->toArray();//根据feed_id,machine_id,seat_code三个字段确定唯一feed_station_id
        if ($feedDetail['sent_count'] > $feedDetail['total_count']) {
            $this->logger->warn("当前物料已发满,工单号：{$feed['feed_id']}，机器号：{$machine['machine_id']}，站位号：{$machineSeat['seat_code']}，已发数量{$feedDetail['sent_count']},总共所需数量：{$feedDetail['total_count']}");
            return $this->resultTemplate(2, ['feedDetailUuid' => $feedDetail['uuid']]);//当前物料已发满
        }
        $this->logger->info("对物料进行续料,工单号：{$feed['feed_id']}，机器号：{$machine['machine_id']}，站位号：{$machineSeat['seat_code']}，当前reelId{$machineSeat['reel_id']}已发数量{$feedDetail['sent_count']},总共所需数量：{$feedDetail['total_count']}");
        return $this->resultTemplate(1, $feedDetail['uuid']);//当前物料未发满

    }

    /***
     * @param $feedDetailUuid
     * @param $isEnough
     * @param $userId
     * @param string $storageUuid
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException 续料操作
     * @throws \Exception
     */
    public function addMateriel($feedDetailUuid, $isEnough, $userId, $storageUuid = "184928afb9e35f3ff421da05fc3f917f")
    {
        $lock = new FileLock($storageUuid);
        $lock->lock(FileLock::LOCK_TYPE_MACHINE_ADD);//加文件锁
        $this->materielFile($userId, $storageUuid);
        $data = json_decode($this->file->read(), true);

        $feedDetail = $this->feedDetailModel->findFeedDetail(['uuid' => $feedDetailUuid])->toArray();//当前补料站位对应的料站细表
        $destinationMachine = $this->machineModel->findMachineInfo(['machine_id' => $feedDetail['machine_id']])->toArray();//

        $this->machineSeatModel->updateMachineSeat(['machine_uuid' => $destinationMachine['uuid'], 'seat_code' => $feedDetail['seat_code']], ['status' => 0]);//将补料的站位锁定，防止出现从机器上剪取物料时，剪取自己的情况
//        if($isEnough == 0){//物料仍未发够

        $reelObject = $this->reelsModel->findReel($feedDetail['sub_materiel_id'], $storageUuid);
        if (empty($reelObject)) {//货架上没有物料了
            $machineWhere['sub_materiel_id'] = $feedDetail['sub_materiel_id'];
            $machineSeatObject = $this->machineSeatModel->selectMachineSeat(['sub_materiel_id' => $feedDetail['sub_materiel_id'], 'status' => 1]);//寻找非锁定状态的物料 todo:这边没有对仓库进行判断，之后再加字段吧
            if (!count($machineSeatObject)) {//货架上没有物料且机器上也没有可用物料
                $this->machineSeatModel->updateMachineSeat(['machine_uuid' => $destinationMachine['uuid'], 'seat_code' => $feedDetail['seat_code']], ['status' => 1]);//将该补料站位解锁，前面三个是条件
                $this->logger->warn('未找到可用物料');
                $lock->unlock();
                return $this->resultTemplate(1);//报错，提示稍后再试
            } else {//机器上有可用物料

                $machineAddResult = $this->setMachineAdd($data, $machineSeatObject, $feedDetail, $isEnough);//锁定要剪取物料的机器，将他的数据插入到文件中

                $lock->unlock();
                return $machineAddResult;
            }
        }
        else {//货架上依旧有物料

            $result = $this->setShelveAdd($data, $reelObject, $storageUuid, $feedDetail, $isEnough);

            $lock->unlock();
            return $result;
        }
//        }
//        else{//发够数量仍发送
////            Db::startTrans();
////            $this->smtMaterielModel->findByLock(['sub']);
//            $this->machineSeatModel->updateMachineSeat(['machine_uuid'=>$destinationMachine['uuid'],'seat_code' => $feedDetail['seat_code']],['status' => 1]);//将该补料站位解锁
//            return $this->resultTemplate(4);
//        }
    }

    /****
     *对续料进行出库
     * @param $feedDetailUuid
     * @param $outputNum
     * @param $userId
     * @param $fromShelves
     * @param string $storageUuid
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \Exception
     */
    public function addMaterielOutput( $feedDetailUuid, $outputNum, $userId, $fromShelves, $storageUuid = "184928afb9e35f3ff421da05fc3f917f")
    {
        $lock = new FileLock($storageUuid);
        $lock->lock(FileLock::LOCK_TYPE_MACHINE_ADD);//加文件锁
        $this->materielFile($userId, $storageUuid);
        $data = json_decode($this->file->read(), true);//获取出库文件
        if ($fromShelves == 1)//如果是货架出库
        {
            foreach ($data['reelsData'] as $key => $item) {//寻找对应的reel的feedAddMateriel文件
                if ($feedDetailUuid == $item['feedDetailUuid']) {
                    $outputData = $item;
                    //将reel的记录添加到transfer中，删除货架中的reel的记录
                    $reelObject = $this->reelsModel->findReelByIdAndUuid($outputData['reelId'], $storageUuid,0);
                    if (empty($reelObject)) {//该物料不存在
                        return $this->resultTemplate(2, $data);
                    }

                    else {//该物料存在
                        $reel = $reelObject->toArray();
                        $this->reelsModel->deleteByUuidList([$reel['uuid']]);
                        $seatCode = $reel['seat_code'];
                        unset($reel['time']);
                        unset($reel['seat_code']);
                        unset($reel['container_code']);//去掉这些字段是因为transfer表不需要这些字段

                        $reel['feed_detail_uuid'] = $feedDetailUuid;
                        $reel['status'] = 0;
                        $this->transferModel->add($reel);//增加transfer表数据
                        $where['uuid'] = $feedDetailUuid;
                        $feedDetail = $this->feedDetailModel->findFeedDetail($where)->toArray();//寻找feedDetail数据

                        $sentCount = $feedDetail['sent_count'] + $reel['count'];//增加已发送数量
                        $data['reelsData'][$key]['sentCount'] = $sentCount; //修改文件的sentCount字段
                        $data['reelsData'][$key]['isOutput'] = true;

                        $this->feedDetailModel->updateFeedDetail(['uuid' => $feedDetailUuid], ['sent_count' => $sentCount]);//更新已发送字段数量
                        $this->smtMaterielModel->startTrans();//事务操作
                        $smtMateriel = $this->smtMaterielModel->findByLock(['sub_materiel_id' => $reel['sub_materiel_id']])->toArray();//减当前货架上物料的数量
                        $smtMateriel['total_num'] = $smtMateriel['total_num'] - $reel['count'];
                        $this->smtMaterielModel->updateMaterielWithLock(['total_num' => $smtMateriel['total_num']], ['sub_materiel_id' => $reel['sub_materiel_id']]);
                        $this->smtMaterielModel->commit();//提交
                        $SmtOutflow = [//写smt出库流水
                            'uuid' => md5(create_uuid()),
                            'reel_id' => $outputData['reelId'],
                            'count' => $outputData['reelCount'],
                            'sub_materiel_id' => $reel['sub_materiel_id'],
                            'feed_id' => $feedDetail['feed_id'],
                            'machine_id' => $outputData['destinationMachine'],
                            'seat_code' => $outputData['destinationSeatCode'],
                            'storage_uuid' => $storageUuid,
                            'user_login' => $userId,
                            'remark' => '货架续料出库'
                        ];
                        $this->smtOutflowModel->addOutFlow($SmtOutflow);//增加smt出库流水

                    }

                    $this->file->write(json_encode($data));
                    $lock->unlock();
                    $this->logger->info("续料流程：货架出库成功。feedDetailUuid：{$feedDetailUuid}。站位号：{$seatCode}。物料条码：{$outputData['reelId']}。数量：{$reel['count']}");
                    return $this->resultTemplate(1,$data);
                }
            }


        } else {//如果是机器剪料
            foreach ($data['machineData'] as $key => $item) {//寻找对应的reel出库记录
                if ($feedDetailUuid == $item['feedDetailUuid']){
                    $machine = $this->machineModel->findMachineInfo(['machine_id' => $item['destinationMachine']])->toArray();
                    $addMachineSeat = $this->machineSeatModel->findMachineSeatInfo(['machine_uuid' => $machine['uuid'],'seat_code' => $item['destinationSeatCode']])->toArray();
                    $data['machineData'][$key]['machineSentCount'] -= $outputNum;
                    $data['machineData'][$key]['sentCount'] += $outputNum;
                    $data['machineData'][$key]['usableCount'] -= $outputNum;
                    $data['machineData'][$key]['isOutput'] = true;
                    $update1 = [
                        'ms.count' => $addMachineSeat['count'] + $outputNum,
                        'ms.status' => 1
                    ];
                    $update2 = [
                        'ms.count' => $item['machineReelCount'] - $outputNum,
                        'ms.status' => 1
                    ];

                    $this->machineSeatModel->updateByMachineIdAndSeat($item['destinationMachine'],$item['destinationSeatCode'],$update1);//更新补料站位上的数量和解锁
                    $this->machineSeatModel->updateByMachineIdAndSeat($item['machineId'],$item['seatCode'],$update2);//更新被剪物料的数量和解锁

                    $this->feedDetailModel->updateFeedDetail(['uuid' => $feedDetailUuid],['sent_count' => $data['machineData'][$key]['sentCount']]);//续料物料的已发数量

                    $this->feedDetailModel->updateFeedDetail(['uuid' => $item['machineCutUuid']],['sent_count' =>$data['machineData'][$key]['machineSentCount']]);//被剪物料的已发数量

                    $this->file->write($data);
                    $lock->unlock();

                    $this->logger->info("续料流程：机器剪料成功。目的feedDetailUuid：{$feedDetailUuid}。
                    从机器：{$item['machineId']} 站位：{$item['seatCode']} 剪取数量：{$outputNum}的物料放入
                    到机器：{$item['destinationMachine']}站位：{$item['destinationSeatCode']}中
                    ");
                    return $this->resultTemplate(3 , $data);
            }
            }
        }
    }

    /***
     * @param $userId
     * @param string $storageUuid
     * @return array
     *续料结束
     */
    public function addMaterielOver($userId,$storageUuid = "184928afb9e35f3ff421da05fc3f917f"){
        $lock = new FileLock($storageUuid);
        $lock->lock(FileLock::LOCK_TYPE_MACHINE_ADD);//加文件锁
        $this->materielFile($userId, $storageUuid);
        $data = json_decode($this->file->read(), true);//获取出库文件
        $reelsOver = array_column($data['reelsData'], 'isOutput');
        $reelsEnd = in_array(false, $reelsOver);
        $machineOver = array_column($data['machineData'], 'isOutput');
        $machineEnd = in_array(false, $machineOver);//若为真则表示未结束
        if ($reelsOver == NULL && $machineOver == NULL){
            return $this->resultTemplate(3);//续料为空？
        }
        else if ($machineEnd == true && $reelsEnd == true){
            return $this->resultTemplate(5);//结束失败，机器续料和货架续料都未结束
        }
        else if($machineEnd == true){
            return $this->resultTemplate(1);//结束失败，机器续料未结束
        }
        else if($reelsEnd == true){
            return $this->resultTemplate(2);//结束失败，货架续料未结束
        }
        else{
            $this->file->delete();
            $this->logger->info("用户{$userId}结束续料成功");
            $lock->unlock();
            return $this->resultTemplate(4);//结束成功

        }

    }


    /***
     * @param $username
     * @param $storageUuid
     * @return string
     * 获取补料文件的地址
     */
    public function getAddMaterielPath($username, $storageUuid)
    {
        $this->storageUuid = $storageUuid;
        $AddMaterielPath = ROOT_PATH . Constant::RECORD_FEED_ADD_MATERIEL_POSITION . $storageUuid . '/' . $username . Constant::RECORD_FILE_SUFFIX;
        return $AddMaterielPath;
    }
//
//    public function searchInTransfer($subMaterielId)
//    {
//        $where['sub_materiel_id'] = $subMaterielId;
//        $this->transferModel->findReel($where);
//
//    }

    /***
     * @param $name
     * @param $storageUuid
     * 续料文件地址：feedAddMateriel
     */
    public function materielFile($name, $storageUuid)
    {
        $this->storageUuid = $storageUuid;
        $filePath = Constant::RECORD_FEED_ADD_MATERIEL_POSITION . $name . Constant::RECORD_FILE_SUFFIX;
        $this->file = new CommonFile($storageUuid, $filePath);

    }

    /***
     *
     * 添加机器续料
     * @param $data
     * @param $machineSeatObject
     * @param $feedDetail
     * @param $isEnough
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function setMachineAdd($data, $machineSeatObject, $feedDetail, $isEnough){
        $machineSeat = $machineSeatObject->toArray();
        foreach ($machineSeat as $item) {//遍历找到的机器站位
            $where['uuid'] = $item['machine_uuid'];
            $machine = $this->machineModel->findMachineInfo($where)->toArray();
            $feed = $this->feedStationModel->findFeed(['uuid' => $machine['feed_uuid']], 'feed_id')->toArray();
            $feedDetailCondition['feed_id'] = $feed['feed_id'];
            $feedDetailCondition['machine_id'] = $machine['machine_id'];
            $feedDetailCondition['seat_code'] = $item['seat_code'];
            $result = $this->feedDetailModel->findFeedDetail($feedDetailCondition)->toArray();//被剪取物料锁对应的feedDetail
            if ($result['total_count'] < $result['sent_count']) {//总需要数量小于已发数量，说明可以从这边剪取物料
                $this->machineSeatModel->updateMachineSeat(['uuid' => $item['uuid']], ['status' => 0]);
                $usableCount = $result['sent_count'] - $result['total_count'];
                $data['machineData'][] = [
                    'feedDetailUuid' => $feedDetail['uuid'],//补料应的feedDetail uuid
                    'destinationMachine' => $feedDetail['machine_id'],//补料的机器
                    'destinationSeatCode' => $feedDetail['seat_code'],//补料的站位
                    'reelId' => $item['reel_id'],//剪料站位上的reel_id
                    'seatCode' => $item['seat_code'],//剪料站位的站位的站位号
                    'machineId' => $machine['machine_id'],//剪料站位的机器号
                    'machineReelCount' => $item['count'],//剪料站位的数量
                    'machineTotalCount' => $result['total_count'],//剪料站位上所需的数量
                    'machineSentCount' => $result['sent_count'],//剪料站位上已发的数量
                    'totalCount' => $feedDetail['total_count'],//补料机器上所需的数量
                    'sentCount' => $feedDetail['sent_count'],//补料站位上已发的数量
                    'usableCount' => $usableCount ,//可以剪取的物料的数量
                    'isEnough' => $isEnough,//是否已经发够物料
                    'machineCutUuid' => $result['uuid'],//被剪取物料的feedDetail所对应的uuid
                    'isOutput' => false,

                ];

                $this->file->write($this->jsonDecode($data));

//                        $lock->unlock(FileLock::LOCK_TYPE_MACHINE_ADD);
                $this->logger->info("续料流程：生成机器剪料的路径。feedDetailUuid：{$feedDetail['uuid']}。从机台：{$machine['machine_id']} 站位：{$item['seat_code']}
                        剪取物料到 机器号： {$feedDetail['machine_id']} 站位号：{$feedDetail['seat_code']}中。可剪取数量:{$usableCount}。物料ID：{$feedDetail['sub_materiel_id']}");
                return $this->resultTemplate(2, $data);
            }
        }
    }


    /***
     * 添加货架续料信息
     * @param $data
     * @param $reelObject
     * @param $storageUuid
     * @param $feedDetail
     * @param $isEnough
     * @return array
     */
    public function setShelveAdd($data, $reelObject, $storageUuid, $feedDetail, $isEnough){
        $reel = $reelObject->toArray();
        $this->reelsModel->updateReel($reel['reel_id'], $storageUuid, ['status' => 0]);

        $data['reelsData'][] = [
            'feedDetailUuid' => $feedDetail['uuid'],//补料对应的feedDetail uuid
            'destinationMachine' => $feedDetail['machine_id'],//要补料的机器号
            'destinationSeatCode' => $feedDetail['seat_code'],//要补料的站位号
            'reelId' => $reel['reel_id'],//reelId
            'seatCode' => $reel['seat_code'],//物料站位
            'containerCode' => $reel['container_code'],//容器号
            'reelCount' => $reel['count'],//当前reel的数量
            'totalCount' => $feedDetail['total_count'],//所需总数量
            'sentCount' => $feedDetail['sent_count'],//已发数量
            'isOutput' => false,//是否已出库
            'materiel' => $reel['sub_materiel_id'],//物料id
            'machineSeat' => $feedDetail['machine_id'] . '#' . $feedDetail['seat_code'],//机器号#站位号
            'isEnough' => $isEnough,//是否已经发够物料 1为是 2为否

        ];
        if(empty(  $data['machineData']) || $data['machineData'] == null){
            $data['machineData'] = Null;
        }
        $this->file->write($this->jsonDecode($data));

//            $lock->unlock(FileLock::LOCK_TYPE_MACHINE_ADD);
        $this->logger->info("续料流程：生成货架取料的路径。feedDetailUuid：{$feedDetail['uuid']}。物料站位：{$reel['seat_code']} 容器号：{$reel['container_code']}
                        目的机器号： {$feedDetail['machine_id']} 站位号：{$feedDetail['seat_code']}中。reelId{$reel['reel_id']}。物料ID：{$feedDetail['sub_materiel_id']}");
        return $this->resultTemplate(3, $data);
    }
}